package edu.hawaii.myisern.action;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.RedirectResolution;

/**
 * ActionBean class for the login screen functionality.
 * @author Ben
 *
 */
@SuppressWarnings( { "unchecked" } )
public class LoginActionBean implements ActionBean {

  /** Login name user types. */
  private String userName;
  /** Password that user types in. */
  private String password;
  /** Contains login errors, if any. */
  private String loginError = "";
  /** Context variable for the session. */
  private MyIsernContext context;

  /**
   * Sets the context. Required by the interface. 
   * @param context The ActionBeanContext to set. 
   */
  public void setContext(ActionBeanContext context) {
    this.context = (MyIsernContext) context;
  }

  /**
   * Returns the context. Required by the interface. 
   * @return the ActionBeanContext.
   */
  public MyIsernContext getContext() {
    return context;
  }

  /**
   * sets the user name from login screen
   * @param name typed into login screen by user.
   */
  public void setUserName(String name) {
    this.userName = name;
  }

  /**
   * sets password from login screen.
   * @param text typed in password field in login screen.
   */
  public void setPassword(String text) {
    this.password = text;
  }

  /**
   * gets the login error message, which may be empty.
   * @return String containing the login error message.
   */
  public String getLoginError() {
    return this.loginError;
  }

  /**
   * Sets the error message for the login screen.
   * @param message contains any error messages.
   */
  public void setLoginError(String message) {
    this.loginError = message;
  }

  /**
   * Handler for the login action at the login page.  Opens the file and compares
   * the login/password the user typed with the file.  
   * @return resolution to displayTable if correct, otherwise back to login.jsp.
   */
  public Resolution login() {
    String user = "";
    String password = "";
    try {
      BufferedReader in;
      in = new BufferedReader(new InputStreamReader(new FileInputStream(
          System.getProperty("user.home") + "/.myisern/login.txt")));

      user = in.readLine();
      password = in.readLine();
      in.close();
    }
    catch (Exception e) {
      this.loginError = "Error opening file: " + e.toString();
      return new ForwardResolution("/login.jsp");
    }
    if (user.equals(this.userName) && password.equals(this.password)) {
      this.context.setLoggedIn(true);
      return new RedirectResolution("/displayTable.jsp");
    }
    else {
      this.loginError = "Invalid Username/Password.";
      return new ForwardResolution("/login.jsp");
    }
  }

  /**
   * Logs a user out of the system.
   * @return Resolution back to login page.
   */
  public Resolution logout() {
    this.context.setLoggedIn(false);
    return new ForwardResolution("/login.jsp");
  }

}
